package cn.net.lwj.singledb.service.impl;

import cn.net.lwj.common.model.JsonResult;
import cn.net.lwj.common.service.Impl.BaseServiceImpl;
import cn.net.lwj.singledb.interceptor.SOAPHeaderInterceptor;
import cn.net.lwj.singledb.service.WsService;
import cn.net.lwj.singledb.webService.sjjhClient.SjjhWebService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.soap.SOAPBinding;
import org.apache.commons.io.FilenameUtils;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 测试事务管理
 */
@Service("wsService")
@SuppressWarnings({"javadoc"})
public class WsServiceImpl extends BaseServiceImpl implements WsService {

    @Value("${sjtb_ws_addr:''}")
    private String sjtbWsAddr;


    /**
     * 源文件路径
     */
    @Value("${ywjpath:''}")
    private String ywjpath;

    /**
     * 下载路径
     */
    @Value("${xzpath:''}")
    private String xzpath;



    public SjjhWebService getSjjhWebService() {
        //创建JAX工厂类
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        //factory.setAddress("http://localhost:8092/sjtb_ww/webservice/sjjh?wsdl");
        factory.setAddress(sjtbWsAddr);
        //factory.setAddress("http://localhost:8030/sjtb_ww/webservice/sjjh?wsdl");
        //传入要调用的对象类
        factory.setServiceClass(SjjhWebService.class);
        factory.getOutInterceptors().add(new SOAPHeaderInterceptor("000", "0", "222222222222"));

        //获取调用接口类的实现对象
        SjjhWebService wss = (SjjhWebService) factory.create();
        return wss;
    }

    @Override
    public JsonResult wsUpload(String zipName) {
        SjjhWebService sjjhWebService = getSjjhWebService();
        org.apache.cxf.endpoint.Client proxy = ClientProxy.getClient(sjjhWebService);
        HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
        HTTPClientPolicy policy = new HTTPClientPolicy();
        long timeout = 10 * 60 * 1000;//
        policy.setConnectionTimeout(timeout);
        policy.setReceiveTimeout(timeout);
        conduit.setClient(policy);
        if (zipName == null) {
            throw new RuntimeException("fileName参数不能为空！");
        }
        //sjjhWebService.downloadWsssxfZip("");
        String wjlj = "F:\\ftpFile\\jhptUpload\\20211129131759_0_6a4fb02a2d414180816cda81f3e74b9btest4_2003_export.zip";
        wjlj = "F:\\ftpFile\\jhptUpload\\20211129131759_0_6a4fb02a2d414180816cda81f3e74b9btest4_2004_export.zip";//243M 成功
        wjlj = "F:\\ftpFile\\jhptUpload\\20211129131759_0_6a4fb02a2d414180816cda81f3e74b9btest4_2001_export.zip";//446M 成功
        //wjlj = "F:\\ftpFile\\jhptUpload\\20211129131759_0_6a4fb02a2d414180816cda81f3e74b9btest4_2005_export.zip";//486M成功
        wjlj = "F:\\ftpFile\\jhptUpload\\20211129131759_0_6a4fb02a2d414180816cdideaexe_1001_export.zip";//728M 成功 exe
        //wjlj = "F:\\ftpFile\\jhptUpload\\20211129131759_0_6a4fb02a2d414180816cda81f3e74b9btest4_2006_export.zip";//608M 成功
        //wjlj = "F:\\ftpFile\\jhptUpload\\20211129131759_0_testmp4jpg419M_2003_export.zip";//348M 成功
        wjlj = "F:\\ftpFile\\jhptUpload\\test1mp4.zip";//348M 成功

        wjlj = ywjpath + File.separator + zipName;
        DataHandler dataHandler = getDataHandler(wjlj);
        String fileName = FilenameUtils.getName(wjlj);

        BindingProvider bp = (BindingProvider) sjjhWebService;
        SOAPBinding binding = (SOAPBinding) bp.getBinding();
        binding.setMTOMEnabled(true);

        String upload = sjjhWebService.uploadWsssxfZip(dataHandler, fileName);
        System.out.println(upload);
        return JsonResult.responseSuccess("上传成功！");
    }

    @Override
    public JsonResult wsDownload(String xfbs, String fileName) {
        if (xfbs == null) throw new RuntimeException("xfbs参数不能为空！");
        if (fileName == null) throw new RuntimeException("fileName参数不能为空！");
        //if (savePath == null) throw new RuntimeException("savePath参数不能为空！");
        SjjhWebService sjjhWebService = getSjjhWebService();
        /*String xml = "<condition>\n" +
                "        <xfbs>6a4fb02a2d414180816cda81f3e74b9btest4</xfbs>\n" +
                "        <zipName>20211118143239_0_1dafb5a7456a45fdbe2f592606d602a7_1004_export.zip</zipName>\n" +
                "    </condition>";*/
        String xml = "<condition>\n" +
                "        <xfbs>@XFBS@</xfbs>\n" +
                "        <zipName>@ZIPNAME@</zipName>\n" +
                "    </condition>";
        xml = xml.replace("@XFBS@", xfbs).replace("@ZIPNAME@", fileName);
        DataHandler handler = sjjhWebService.downloadWsssxfZip(xml);
        InputStream input = null;
        FileOutputStream fos = null;
        try {
            input = handler.getInputStream();
            //fos = new FileOutputStream("F:\\ftpFile\\test\\20211118143239_0_1dafb5a7456a45fdbe2f592606d602a7_1004_export.zip");
            fos = new FileOutputStream(xzpath + File.separator + fileName);
            byte[] buffer = new byte[4096];
            int length = 0;
            while ((length = input.read(buffer)) != -1) {
                fos.write(buffer, 0, length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("2222222222");
        return JsonResult.responseSuccess("下载成功！");

    }

    public static DataHandler getDataHandler(String wjlj) {
        DataHandler dataHandler = new DataHandler(new FileDataSource(wjlj));
        System.out.println("-------------------------下载");
        return dataHandler;
    }
}
